//
//  TableTextFieldViewCell.m
//  FCTools
//
//  Created by WJC on 2018/10/25.
//  Copyright © 2018 wjc. All rights reserved.
//

#import "TableTextFieldViewCell.h"
#import "WJCDefine.h"
@interface TableTextFieldViewCell()<UITextFieldDelegate>
@end
@implementation TableTextFieldViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(16, 12, 150, 20)];
        self.titleLabel.font=font15;
        self.titleLabel.textColor=HEXC(0x232323);
       
        self.textField=[[UITextField alloc]initWithFrame:CGRectMake(self.titleLabel.frame.origin.x+self.titleLabel.frame.size.width+16, 6, WJCScreenW-(self.titleLabel.frame.origin.x+self.titleLabel.frame.size.width+32), 35)];
        self.textField.font=font15;
        self.textField.textColor=HEXC(0x232323);
        self.textField.textAlignment=NSTextAlignmentRight;
        self.textField.delegate=self;
        self.titleLabel.text=@"";
        self.textField.text=@"";
        
        [self.contentView addSubview: self.titleLabel];
        [self.contentView addSubview: self.textField];
        self.countLabel=[[UILabel alloc]initWithFrame:CGRectMake(16, FCScaleHeight(48)-20*0.5, 150, 20)];
        self.countLabel.font=font13;
//        if (IphonePlus) {
//            self.titleLabel.y=15;
//            self.textField.y=15;
//            self.titleLabel.font=font17;
//            self.textField.font=font17;
//            self.countLabel.font=font15;
//        }
        self.countLabel.textColor=HEXC(0x646262);
        [self.contentView addSubview:self.countLabel];
        [self.countLabel sizeToFit];
        self.countLabel.hidden=YES;
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
                                                    name:@"UITextFieldTextDidChangeNotification"
                                                  object:self.textField];
        [self.textField addTarget:self action:@selector(inputExchanged:) forControlEvents:UIControlEventEditingChanged];
        [self.textField addTarget:self action:@selector(inputEnd:) forControlEvents:UIControlEventEditingDidEnd];
        self.lineView=[UIView new];
        self.lineView.frame=CGRectMake(0, 0, WJCScreenW, 1);
        self.lineView.backgroundColor=HEXC(0xD8D8D8);
        [self.contentView addSubview:self.lineView];
        self.lineView.hidden =YES;
    }
    return self;
}
- (void)inputEnd:(UITextField *)sender {
    
    if (self.txtEndBlock) {
        self.txtEndBlock(self.titleLabel,sender);
    }
    
}
- (void)inputExchanged:(UITextField *)sender {
    
    if (self.txtBlock) {
        self.txtBlock(self.titleLabel,sender);
    }
    
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)setfit{
    [self.titleLabel sizeToFit];
    self.titleLabel.y=(self.contentView.height-self.titleLabel.height)*0.5;
    self.textField.frame=CGRectMake(self.titleLabel.frame.origin.x+self.titleLabel.frame.size.width+16, 0, self.width-(self.titleLabel.frame.origin.x+self.titleLabel.frame.size.width+32), self.contentView.height>0?self.contentView.height:FCScaleHeight(48));
    if (!self.countLabel.hidden) {
        self.countLabel.frame=CGRectMake(WJCScreenW-32, 8, 50, 16);
        [self.countLabel sizeToFit];
        self.textField.frame=CGRectMake(self.titleLabel.frame.origin.x+self.titleLabel.frame.size.width+16+self.countLabel.frame.size.width, 0, self.width-(self.titleLabel.frame.origin.x+self.titleLabel.frame.size.width+32), self.contentView.height>0?self.contentView.height:FCScaleHeight(48));
    }
    if (self.accessoryView) {
        self.textField.frame=CGRectMake(self.titleLabel.frame.origin.x+self.titleLabel.frame.size.width+16, 0, self.width-(self.titleLabel.frame.origin.x+self.titleLabel.frame.size.width+32+self.accessoryView.frame.size.width+16), self.contentView.height>0?self.contentView.height:FCScaleHeight(48));
        
    }
    self.titleLabel.centerY = FCScaleHeight(22);
    self.textField.centerY = FCScaleHeight(22);
//    if (IphonePlus) {
//        self.titleLabel.y=15;
//        self.textField.height=54;
//        self.titleLabel.font=font17;
//        self.textField.font=font17;
//        self.countLabel.font=font15;
//    }
}
-(void)textFiledEditChanged:(NSNotification *)obj{
    UITextField *textField = (UITextField *)obj.object;
        NSString *toBeString = textField.text;
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    

    if ([lang isEqualToString:@"zh-Hans"]){//[NSString returnCurrentLanguage]==languageZH) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
            //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
            // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (self.countLabel.hidden==NO) {
                
                if (toBeString.length > self.maxCount ){
                    self.countLabel.textColor=[UIColor redColor];
                    textField.text = [toBeString substringToIndex:self.maxCount];
                    self.countLabel.text=[NSString stringWithFormat:@"%ld/%ld", textField.text.length,(long)self.maxCount];
                }else{
                    self.countLabel.textColor=[UIColor lightGrayColor];
                    self.countLabel.text=[NSString stringWithFormat:@"%ld/%ld", textField.text.length,(long)self.maxCount];
                }
                if (self.txtBlock) {
                    self.txtBlock(self.titleLabel,textField);
                }
            }
        }
            // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
            
        }
    }
        // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (self.countLabel.hidden==NO) {
            if (toBeString.length > self.maxCount*4) {
                textField.text = [toBeString substringToIndex:self.maxCount*4];
                self.countLabel.textColor=[UIColor redColor];
                self.countLabel.text=[NSString stringWithFormat:@"%ld/%ld",textField.text.length,self.maxCount*4];
            }else{
                self.countLabel.textColor=[UIColor lightGrayColor];
                self.countLabel.text=[NSString stringWithFormat:@"%ld/%ld",textField.text.length,self.maxCount*4];
            }
            if (self.txtBlock) {
                self.txtBlock(self.titleLabel,textField);
            }
        }
        
    }
    }
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
